// comment.js
const requester = require('../../common/requester.js');
const EventEmitter = require('../../common/EventEmitter.js');
const config = require('../../common/config.js');
const app = getApp();

Page({
    data: {
        inputStatus: false,
        sendButtonEnable: false,
        messageList: [],
        result: {},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.resourceId = options.resourceId;
        this.getData(false);
        let me = this;
        wx.getStorage({
            key: config.storage.keyUserInfo,
            success: function (res) {
                me.setData({
                    userInfo: res
                })
            }
        });
    },


    /**
     * 获取当前选中Tab的数据。
     * 当加载更多时,也调用此方法。页面触及到底部时,调用该方法。
     * 该方法自动判断当前是哪一个Tab,并调用改Tab对应的方法去获取对应的数据。
     * 并取出页面的页面值,一次添加。
     */
    getData: function (isLoadMore) {
        if (isLoadMore) {
            this.pageIndex++;
        } else {
            this.pageIndex = 1;
        }

        this.getCommentDataList()
            .then(li => {
                if (li) {
                    this.setData({
                        messageList: li.reverse()
                    });
                }
                if (this.pageIndex === 2) {
                    this.setData({
                        bottomId: this.bottomId
                    })
                } else if (isLoadMore) {
                    let id = li.length % 100;
                    id = id ? id : 100;
                    this.setData({
                        bottomId: 'msg' + id
                    })
                }
                setTimeout(() => {
                    this.osScrolltoupper = false;
                }, 300)
            });
    },

    scrollToUpper: function () {
        if (this.osScrolltoupper) {
            return;
        }
        this.osScrolltoupper = true;
        this.getData(true);
    },

    /**
     * 获取评论数据列表
     */
    getCommentDataList: function () {
        let me = this;
        let commentType = 2;
        let pageIndex = this.pageIndex;
        return requester.request({
            url: requester.service.commentList,
            data: {
                resourceId: me.data.resourceId,
                commentType: commentType,
                pageIndex: pageIndex || 1,
                pageSize: 100
            }
        }).then(res => {
            if (res.data && ((res.code != 0) || (!res.data.length))) {
                return;
            }
            let comments = res.data;
            // If the page index is 1, then the
            let tmpLi = me.data.messageList || [];
            if (pageIndex === 1) {
                tmpLi = [];
                me.bottomId = comments ? "msg" + Number(comments.length - 1) : "msg0"
            }
            comments = (comments && tmpLi.reverse().concat(comments));
            return comments;
        });
    },

    // 输入评论
    commentInput: function (e) {
        let inputContent = e.detail.value.replace(/ /g, "");
        let str = inputContent ? inputContent.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "") : "";
        this.setData({
            commentContent: str,
            sendButtonEnable: !!inputContent
        });
    },

    // 发送评论
    sendComment: function (e) {
        let me = this;
        let comment = this.data.commentContent;
        this.setData({
            inputStatus: false
        });

        if (!comment || !this.data.sendButtonEnable) {
            return;
        }

        return requester.request({
            url: requester.service.favorComment,
            data: {
                resourceId: me.data.resourceId,
                commentType: '2',
                content: comment
            }
        }).then(res => {
            if (res.code == 0) {
                me.getData(false);
            }
            me.setData({
                commentContent: ""
            });
            EventEmitter.dispatch('backFormComment');
        });
    },

    // 进入用户详情页面
    goUserDetail: function (e) {
        let userId = e.currentTarget.dataset.userId;
        wx.navigateTo({
            url: "../me/me?userId=" + userId
        });
    }
});